package com.cloud.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cloud.mapper.AdminMapper;
import com.cloud.mapper.AreaMapper;
import com.cloud.mapper.LoginLimitMapper;
import com.cloud.mapper.TypeLoginMapper;
import com.cloud.mapper.UsersDataMapper;
import com.cloud.mapper.UsersMapper;
import com.cloud.mapper.UsersPhoneCopyMapper;
import com.cloud.mapper.UsersPhoneMapper;
import com.cloud.pojo.Admin;
import com.cloud.pojo.AdminExample;
import com.cloud.pojo.AdminExample.Criteria;
import com.cloud.pojo.Area;
import com.cloud.pojo.LoginLimit;
import com.cloud.pojo.LoginLimitExample;
import com.cloud.pojo.Page;
import com.cloud.pojo.TypeLogin;
import com.cloud.pojo.TypeLoginExample;
import com.cloud.pojo.Users;
import com.cloud.pojo.UsersData;
import com.cloud.pojo.UsersDataExample;
import com.cloud.pojo.UsersExample;
import com.cloud.pojo.UsersPhone;
import com.cloud.pojo.UsersPhoneCopy;
import com.cloud.pojo.UsersPhoneCopyExample;
import com.cloud.pojo.UsersPhoneExample;
import com.cloud.pojo.UsersWithBLOBs;
import com.cloud.result.DataResult;
import com.cloud.service.AdminService;

@Service
public class AdminServiceImpl implements AdminService {
	
	@Autowired
	private AdminMapper adminMapper;
	@Autowired
	private LoginLimitMapper loginLimitMapper;
	@Autowired
	private TypeLoginMapper typeLoginMapper;
	@Autowired
	private UsersDataMapper usersDataMapper;
	@Autowired
	private UsersMapper usersMapper;
	@Autowired
	private UsersPhoneMapper usersPhoneMapper;
	@Autowired
	private AreaMapper areaMapper;
	@Autowired
	private UsersPhoneCopyMapper usersPhoneCopyMapper;
	
	
	@Override
	public Admin selectAdminByUsername(String username) {
		// TODO Auto-generated method stub
		AdminExample example = new AdminExample();
		Criteria criteria = example.createCriteria();
		criteria.andUsernameEqualTo(username);
		List<Admin> list = adminMapper.selectByExample(example);
		if(list != null && list.size()>0){
			return list.get(0);
		}
		return null;
	}

	@Override
	public Integer updateHardware(Admin ad) {
		// TODO Auto-generated method stub
		return adminMapper.updateByPrimaryKeySelective(ad);
	}

	@Override
	public DataResult<Admin> selectAdminList(Integer page, Integer rows) {
		// TODO Auto-generated method stub
		AdminExample example = new AdminExample();
		int startPos = (page-1)*rows;
		example.setPage(new Page(startPos, rows));
		example.setOrderByClause("create_time desc");
		
		int total = adminMapper.countByExample(example);
		List<Admin> adminList = adminMapper.selectByExamplePage(example);
		
		DataResult<Admin> dataResult = new DataResult<>(total, adminList, null);
		return dataResult;
	}

	@Override
	public boolean unbundling(String ids) {
		// TODO Auto-generated method stub
		// 将字符串ids以逗号分割成id数组
		String[] ids_arr = ids.split(",");
		for (String id : ids_arr) {
			// 根据id查询商品对象
			Integer byId = Integer.valueOf(id);
			Admin admin = adminMapper.selectByPrimaryKey(byId);
			if (admin != null) {
				admin.setHardware("");
				// 再执行更新操
				adminMapper.updateByPrimaryKeySelective(admin);
			} else {
				return false;
			}

		}
		return true;
	}

	@Override
	public int saveAdmin(Admin admin) {
		// TODO Auto-generated method stub
		admin.setCreateTime(new Date());
		return adminMapper.insertSelective(admin);
	}

	@Override
	public Map<String, Object> test() throws Exception {
		// TODO Auto-generated method stub
		/*Map<String, Object> data = new HashMap<>();
		LoginLimitExample example = new LoginLimitExample();
		com.cloud.pojo.LoginLimitExample.Criteria criteria = example.createCriteria();
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date1 = sdf.parse("2017-10-16 15:10:00");
//		Date date2 = sdf.parse("2017-12-31 23:59:59");
		criteria.andStopTimeGreaterThan(date1);
		//criteria.andLoginTypeIdEqualTo(1);
//		criteria.andLoginTypeIdNotEqualTo(1);
//		criteria.andStopTimeLessThan(date2);
//		criteria.andStopTimeBetween(date1, date2);
		List<LoginLimit> list1 = loginLimitMapper.selectByExample(example);
		for (LoginLimit loginLimit : list1) {
			Area area = areaMapper.selectByPrimaryKey(loginLimit.getUserId());
			if(area != null){
				if(area.getProvince().equals("齐河")){
					
			UsersData usersData = new UsersData();
			UsersExample example2 = new UsersExample();
			com.cloud.pojo.UsersExample.Criteria criteria2 = example2.createCriteria();
			criteria2.andIdEqualTo(loginLimit.getUserId());
			List<UsersWithBLOBs> list2 = usersMapper.selectByExampleWithBLOBs(example2);
					
					UsersPhoneExample example3 = new UsersPhoneExample();
					com.cloud.pojo.UsersPhoneExample.Criteria criteria3 = example3.createCriteria();
					criteria3.andUserIdEqualTo(loginLimit.getUserId());
					List<UsersPhone> list3 = usersPhoneMapper.selectByExample(example3);
					StringBuffer sb = new StringBuffer();
					for (UsersPhone usersPhone : list3) {
						sb.append(usersPhone.getPhone()+" ");
					}
					
					TypeLoginExample example4 = new TypeLoginExample();
					com.cloud.pojo.TypeLoginExample.Criteria criteria4 = example4.createCriteria();
					criteria4.andIdEqualTo(loginLimit.getLoginTypeId());
					List<TypeLogin> list4 = typeLoginMapper.selectByExample(example4);
					
					UsersDataExample example5 = new UsersDataExample();
					com.cloud.pojo.UsersDataExample.Criteria criteria5 = example5.createCriteria();
					criteria5.andCertNameEqualTo(list2.get(0).getCertName());
					List<UsersData> list5 = usersDataMapper.selectByExample(example5);
					if(list5 != null && list5.size()>0){
						usersData.setId(list5.get(0).getId());
						usersData.setDescription(list5.get(0).getDescription()+"/"+list4.get(0).getDescription());
						usersData.setStopTime(list5.get(0).getStopTime()+"/"+sdf.format(loginLimit.getStopTime()));
						usersDataMapper.updateByPrimaryKeySelective(usersData);
					}else {
						usersData.setCertName(list2.get(0).getCertName());
						usersData.setPhone(sb.toString());
						usersData.setWorkAddress(list2.get(0).getWorkAddress());
						usersData.setDescription(list4.get(0).getDescription());
						usersData.setStopTime(sdf.format(loginLimit.getStopTime()));
						usersData.setUsername(list2.get(0).getUsername());
						usersData.setCity(area.getProvince());
						int id = usersDataMapper.insert(usersData);
					}*/
					
				/*}
			}
		}*/
		UsersPhoneExample example = new UsersPhoneExample();
		List<UsersPhone> list = usersPhoneMapper.selectByExample(example);
		for (UsersPhone usersPhone : list) {
			UsersPhoneCopyExample copyExample = new UsersPhoneCopyExample();
			com.cloud.pojo.UsersPhoneCopyExample.Criteria criteria = copyExample.createCriteria();
			criteria.andUserIdEqualTo(usersPhone.getUserId());
			List<UsersPhoneCopy> copies = usersPhoneCopyMapper.selectByExample(copyExample);
			if(copies == null || copies.size() == 0){
				UsersPhoneCopy copy = new UsersPhoneCopy();
				copy.setUserId(usersPhone.getUserId());
				copy.setPhone(usersPhone.getPhone());
				usersPhoneCopyMapper.insertSelective(copy);
			}else {
				UsersPhoneCopy phoneCopy = copies.get(0);
				phoneCopy.setPhone(phoneCopy.getPhone()+" "+usersPhone.getPhone());
				usersPhoneCopyMapper.updateByPrimaryKeySelective(phoneCopy);
			}
		}
		
		
		
		return null;
	}
	public Map<String, Object> test2() throws Exception {
		// TODO Auto-generated method stub
		//获取用户id
		UsersExample usersExample = new UsersExample();
		com.cloud.pojo.UsersExample.Criteria userCriteria = usersExample.createCriteria();
		userCriteria.andCertNameNotEqualTo("非正式会员");
		List<Integer> userIdList = usersMapper.selectUserIdList(usersExample);
		//遍历用户id获取登录权限
		for (Integer userId : userIdList) {
			List<LoginLimit> loginLimitList = loginLimitMapper.selectByPrimaryKey2(userId);
				
				
			}
			
			
		
		
		
		
		
		
		
		return null;
	}

}
